package com.qdsgjsfk.vision.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import java.util.List;

import com.qdsgjsfk.vision.R;
import com.qdsgjsfk.vision.model.Point;
import com.qdsgjsfk.vision.util.Constant;

public class PointAdapter extends RecyclerView.Adapter<PointAdapter.Holder> {

    private Context context;
    private ItemClick myItemClick;
    private List<Point> list;
    private int chartType;


    public PointAdapter(Context context, List<Point> list, int chartType, ItemClick myItemClick) {
        this.context = context;
        this.list = list;
        this.myItemClick = myItemClick;
        this.chartType = chartType;
    }


    @NonNull
    @Override
    public Holder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new Holder( LayoutInflater.from( context ).inflate( R.layout.item_point, parent, false ) );
    }

    @Override
    public void onBindViewHolder(@NonNull Holder holder, int position) {
        if (list.get(position).flag) {
            holder.e.setColorFilter(context.getResources().getColor(R.color.mainColor));
        }else{
            holder.e.setColorFilter(context.getResources().getColor(R.color.lightPointColor));
        }
        if (chartType == Constant.CHART_TEACHER_E || chartType == Constant.CHART_TEACHER_E_2_5) {
            holder.e.setImageResource(R.mipmap.point_e);
            rotationImage(holder.e, list.get(position).directionOrType);
        } else if (chartType == Constant.CHART_TEACHER_C) {
            holder.e.setImageResource(R.mipmap.point_c);
            rotationImage(holder.e, list.get(position).directionOrType);
        } else if (chartType == Constant.CHART_TEACHER_ANIMAL) {
            holder.e.setImageResource(list.get(position).resource);
        }
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                for (Point point : list) {
                    point.flag = false;
                }
                list.get(position).flag = true;
                myItemClick.onItemClick(list.get(position).directionOrType);
                notifyItemRangeChanged(0,list.size());
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    static class Holder extends RecyclerView.ViewHolder {
        ImageView e;


        Holder(View itemView) {
            super(itemView);
            e = itemView.findViewById(R.id.e);
        }
    }


    private void rotationImage(ImageView e, int rotation) {
        e.post( () -> {
            e.setPivotX(e.getWidth() / 2);
            e.setPivotY(e.getHeight() / 2);//支点在图片中心
            e.setRotation(rotation);
        } );

    }


    public interface ItemClick {
        void onItemClick(int directionOrType);
    }
}
